$(function(){
	var all={};
	var chedui={
		car:[],
		driver:[]
	};
	$('.back').on('click',function(){
		window.history.go(-1)
	})
	ajaxcar()
	ajaxdriver()
	function ajaxcar(){}
	function ajaxdriver(){
		$.ajax({
		type:"get",
		url:"{$static_dir}/assets/json/chedui.json",
		success:function(res){
			res=JSON.parse(res)
			console.log(res)
			var driver_list="";
			all.driver=[];
			//遍历显示该公司所有司机
			for(var i=0;i<res.driver.length;i++){
				all.driver.push(res.driver[i].driver_id)
				driver_list+=`<tr data-id='${res.driver[i].driver_id}' style='cursor: pointer;'>
								<td>${res.driver[i].driver_id}</td>
								<td>${res.driver[i].age}</td>
								<td>${res.driver[i].driver_no}</td>
								<td>${res.driver[i].idcard}</td>
							</tr>`
			};
			$('.all_driver table').html(driver_list)
			
			$('.all_driver tr').on('click',function(){
				var adddriver=1
				for(var mi=0;mi<chedui.driver.length;mi++){
					if(chedui.driver[mi].driver_id==$(this).data('id')){
						adddriver=0
					}else{
						
					}
				}
				if(adddriver==1){
					for(var mi2=0;mi2<all.driver.length;mi2++){
						if(all.driver[mi2]==$(this).data('id')){
							console.log(all.driver[mi2])
							chedui.driver.push(res.driver[mi2])
						}else{
							
						}
					}
				}
				console.log(chedui.driver)
				var driver_list2="";
				
				//遍历显示车队司机
				for(var i2=0;i2<chedui.driver.length;i2++){
					driver_list2+=`<tr data-id='${chedui.driver[i2].driver_id}' >
									<td>${chedui.driver[i2].driver_id}</td>
									<td>${chedui.driver[i2].age}</td>
									<td>${chedui.driver[i2].driver_no}</td>
									<td>${chedui.driver[i2].idcard}</td>
									<td>
										<div class="tpl-table-black-operation">
											<a href="javascript:;" style='cursor: pointer;padding:0' class="tpl-table-black-operation-del delete">
												<i class="am-icon-trash"></i> 删除
											</a>
										</div>
									</td>
								</tr>`
				};
				$('.my_driver table').html(driver_list2)
				deteledriver()
				function deteledriver(){
				$('.my_driver .delete').on('click',function(){
					for(var di=0;di<chedui.driver.length;di++){
						if(chedui.driver[di].driver_id==$(this).closest('tr').data('id')){
							chedui.driver.splice(di, 1);
						}
					}
					var driver_list2="";
					for(var i2=0;i2<chedui.driver.length;i2++){
					driver_list2+=`<tr data-id='${chedui.driver[i2].driver_id}' >
										<td>${chedui.driver[i2].driver_id}</td>
										<td>${chedui.driver[i2].age}</td>
										<td>${chedui.driver[i2].driver_no}</td>
										<td>${chedui.driver[i2].idcard}</td>
										<td>
											<div class="tpl-table-black-operation">
												<a href="javascript:;" style='cursor: pointer;padding:0' class="tpl-table-black-operation-del delete">
													<i class="am-icon-trash"></i> 删除
												</a>
											</div>
										</td>
									</tr>`
					};
					$('.my_driver table').html(driver_list2)
					deteledriver()
				})
				}
			})
			
			
			
			var car_list="";
			all.car=[]
			for(var i=0;i<res.car.length;i++){
				all.car.push(res.car[i].car_id)
				car_list+=`<tr data-id='${res.car[i].car_id}' style='cursor: pointer;'>
								<td>${res.car[i].number}</td>
								<td>${res.car[i].brand}</td>
								<td>${res.car[i].car_type}</td>
								<td>${res.car[i].time}</td>
							</tr>`
			}
			$('.all_car table').html(car_list)
		}
	});
	}
	
})
